import React from "react"
import { withRouter } from "react-router-dom"
import {Login} from "../../api"
@withRouter
class PhoneLogin extends React.Component{
    constructor(){
        super()
        this.state={
            password:"",
            username:""
        }
    }


    loginname(e){
        // console.log(e.target.value);
        const password1=e.target.value
        this.setState({
            password:password1 
        })
    }
    loginword(e){
        // console.log(e.target.value)
        const name=e.target.value
        this.setState({
            username:name
        })
    }

    login(){
        Login(this.state.password,this.state.username).then(
            res=>{
                if(res.profile){
                    alert("登入成功")
                // console.log(res);
                localStorage.setItem('uid',res.profile.userId)
                localStorage.setItem('login_info',JSON.stringify(res));
                let {history}=this.props;
                history.push("/")
                }else{
                    alert("登入失敗")
                }
               
            }
        )
    }
    render(){
        return(
            <div className="phonelogin">
                 <input type="text"  placeholder="请输入手机号" onChange={this.loginname.bind(this)} className="bb"/>
                 <hr/>
                 <input type="password"  placeholder="请输入密码" onChange={this.loginword.bind(this)} className="bb"/>
                 <hr className="sst_hr"/>
                 <button onClick={this.login.bind(this)} className="deng">
                     登录
                 </button>
            </div>
           
        )
    }
}
export default PhoneLogin